﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Net;
using System.Net.Sockets;
using System.Net.NetworkInformation;

namespace BerkeleyAlgorithm
{
    public static class utils
    {
        public static IPAddress FindLanAddress()
        {
            IPAddress gateway = FindGetGatewayAddress();
            if (gateway == null)
                return null;

            IPAddress[] pIPAddress = Dns.GetHostAddresses(Dns.GetHostName());

            foreach (IPAddress address in pIPAddress)
            {
                if (IsAddressOfGateway(address, gateway))
                    return address;
            }
            return null;
        }

        static bool IsAddressOfGateway(IPAddress address, IPAddress gateway)
        {
            if (address != null && gateway != null)
                return IsAddressOfGateway(address.GetAddressBytes(), gateway.GetAddressBytes());
            return false;
        }

        static bool IsAddressOfGateway(byte[] address, byte[] gateway)
        {
            if (address != null && gateway != null)
            {
                int gwLen = gateway.Length;
                if (gwLen > 0)
                {
                    if (address.Length == gateway.Length)
                    {
                        --gwLen;
                        int counter = 0;
                        for (int i = 0; i < gwLen; i++)
                        {
                            if (address[i] == gateway[i])
                                ++counter;
                        }
                        return (counter == gwLen);
                    }
                }
            }
            return false;
        }

        static IPAddress FindGetGatewayAddress()
        {
            IPGlobalProperties ipGlobProps = IPGlobalProperties.GetIPGlobalProperties();

            foreach (NetworkInterface ni in NetworkInterface.GetAllNetworkInterfaces())
            {
                IPInterfaceProperties ipInfProps = ni.GetIPProperties();
                foreach (GatewayIPAddressInformation gi in ipInfProps.GatewayAddresses)
                    return gi.Address;
            }
            return null;
        }

        public static byte[] StrToByteArray(string str)
        {
            System.Text.UTF8Encoding encoding = new System.Text.UTF8Encoding();
            return encoding.GetBytes(str);
        }

        public static string ByteToStrArray(byte[] word)
        {
            string ret = "";
            for (int i = 0; i < word.Length; i++)
                ret+=Convert.ToChar(word[i]);
            ret = ret.Substring(0, ret.IndexOf('\0'));
            return ret;
        }

        //returns true if first is bigger NOT equal
        public static bool comperUUIDStr(string first, string seconed)
        {            
            if (first.Length > seconed.Length) return true;
            if (first.Length < seconed.Length) return false;

            for (int i = 0; i < first.Length; i++)
            {
                if (first[i] > seconed[i]) return true;
                if (first[i] < seconed[i]) return false;
            }

            return false ;
        }
    }


}
